home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / faxd / HDLCFrame.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  129 lines

  1. /*    $Header: /usr/people/sam/fax/faxd/RCS/HDLCFrame.c++,v 1.11 1994/02/28 14:15:26 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25.  
  26. /*
  27.  * Raw HDLC Frame Interface.
  28.  */
  29. #include "HDLCFrame.h"
  30. #include <string.h>
  31. #include <stdlib.h>
  32.  
  33. HDLCFrame::HDLCFrame(u_int fo)
  34. {
  35.     frameOverhead = fo;
  36.     base = next = buf;
  37.     end = &buf[sizeof(buf)];
  38.     amountToGrowBy = 1024;
  39.     ok = FALSE;
  40. }
  41.  
  42. HDLCFrame::~HDLCFrame()
  43. {
  44.     if (base != buf) delete base;
  45. }
  46.  
  47. HDLCFrame::HDLCFrame(const HDLCFrame& other)
  48. {
  49.     u_int size = other.end - other.base;
  50.     u_int len = other.getLength();
  51.     if (size > sizeof(buf)) {
  52.     base = (u_char*) malloc(size);
  53.     } else {
  54.     base = &buf[0];
  55.     }
  56.     end = base + size;
  57.     next = base + len;
  58.     memcpy(base, other.base, len);
  59.     ok = other.ok;
  60.     frameOverhead = other.frameOverhead;
  61. }
  62.  
  63. void
  64. HDLCFrame::addc(u_char c)
  65. {
  66.     if (next >= end)
  67.     grow(amountToGrowBy);
  68.     *next++ = c;
  69. }
  70.  
  71. void
  72. HDLCFrame::grow(u_int amount)
  73. {
  74.     // insure an acceptable amount of growth
  75.     if (amount < amountToGrowBy) amount = amountToGrowBy;
  76.  
  77.     // move data into larger piece of memory
  78.     u_int size = end - base;
  79.     u_int len = getLength();
  80.     u_int newSize = size + amount;
  81.     if (base == buf) {
  82.     base = (u_char*) malloc(newSize);
  83.     memcpy(base, buf, sizeof(buf));
  84.     } else {
  85.     base = (u_char*) realloc(base, newSize);
  86.     }
  87.  
  88.     // update position pointers
  89.     end = base + newSize;
  90.     next = base + len;
  91. }
  92.  
  93. void
  94. HDLCFrame::put(const u_char* c, u_int len)
  95. {
  96.     u_int remainingSpace = end - next;
  97.     if  (len > remainingSpace)
  98.     grow(len - remainingSpace);
  99.     memcpy(next, c, len);
  100.     next += len;
  101. }
  102.  
  103. u_int
  104. HDLCFrame::getDataWord() const
  105. {
  106.     u_int n = getFrameDataLength();
  107.     u_int w = (n > 1) ? (*this)[3] : 0;
  108.     if (n > 2) w = (w<<8)|(*this)[4];
  109.     if (n > 3) w = (w<<8)|(*this)[5];
  110.     if (n > 4) w = (w<<8)|(*this)[6];
  111.     return w;
  112. }
  113.  
  114. u_int
  115. HDLCFrame::getDIS() const
  116. {
  117.     u_int n = getFrameDataLength();
  118.     u_int dis = (n > 1) ? (*this)[3] : 0;
  119.     dis <<= 8; if (n > 2) dis |= (*this)[4];
  120.     dis <<= 8; if (n > 3) dis |= (*this)[5];
  121.     return dis;
  122. }
  123.  
  124. u_int
  125. HDLCFrame::getXINFO() const
  126. {
  127.     return (getFrameDataLength() > 4 && ((*this)[5] & 0x1)) ? (*this)[6] : 0;
  128. }
  129.